package com.lagou.edu.boss.rest;

import com.alibaba.fastjson.JSON;
import com.lagou.edu.boss.entity.bo.UpLoadResult;
import com.lagou.edu.boss.entity.form.CourseForm;
import com.lagou.edu.boss.entity.vo.CourseVo;
import com.lagou.edu.boss.service.impl.CourseService;
import com.lagou.edu.boss.service.impl.OssService;
import com.lagou.edu.common.entity.vo.Result;
import com.lagou.edu.common.result.ResultCode;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.dto.PageResultDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Author:   mkp
 * Date:     2020/6/21 10:13
 * Description: 课程
 */
@Api(tags = "课程", produces = "application/json")
@Slf4j
@Controller
@RequestMapping("/course/")
public class CourseController {

    @Autowired
    private CourseRemoteService courseRemoteService;
    @Autowired
    private OssService ossService;
    @Autowired
    private CourseService courseService;
    @ApiOperation(value = "保存或者更新课程信息")
    @PostMapping("saveOrUpdateCourse")
    @ResponseBody
    public Result<CourseDTO> saveOrUpdateCourse(@RequestBody CourseForm courseForm){
        log.info("保存或者更新课程信息courseForm:{}", JSON.toJSONString(courseForm));
        try {
            CourseDTO courseDTO = new CourseDTO();
            BeanUtils.copyProperties(courseForm,courseDTO);
            String courseDescriptionMarkDown = courseForm.getCourseDescriptionMarkDown();
            if(StringUtils.isNotBlank(courseDescriptionMarkDown)){
                Document doc = Jsoup.parse(courseDescriptionMarkDown);
                Elements elements = doc.body().select("> *");
                for (Element element : elements) {
                    courseService.removeImgWidthAttribute(element);
                }
                courseDTO.setCourseDescription(doc.body().html());
            }
            this.courseRemoteService.saveOrUpdateCourse(courseDTO);
            return Result.success();
        }catch (Exception e){
            log.error("更新课程信息失败:",e);
            return Result.fail(ResultCode.INTERNAL_ERROR);
        }
    }
    @ApiOperation(value = "通过课程Id获取课程信息")
    @GetMapping("getCourseById")
    @ResponseBody
    public Result<CourseVo> getCourseById(@RequestParam("courseId") Integer courseId)  {
        log.info("获取课程信息:{}",courseId);
        try {
            CourseDTO courseDTO = courseRemoteService.getCourseById(courseId,1);
            log.info("课程信息：{}",JSON.toJSONString(courseDTO));
            CourseVo courseVo = new CourseVo();
            if(courseDTO != null){
                BeanUtils.copyProperties(courseDTO,courseVo);
            }
            log.info("信息 {}",JSON.toJSONString(courseVo));
            return Result.success(courseVo);
        }catch (Exception e){
            log.error("获取课程信息:",e);
            return Result.fail(e.getMessage());
        }
    }
    /**
     *
     */
    @ApiOperation(value = "课程上下架")
    @ApiImplicitParams ({
            @ApiImplicitParam(name = "courseId", value = "课程ID"),
            @ApiImplicitParam(name = "status", value = "课程状态，0-草稿，1-上架")
    })
    @GetMapping("changeState")
    @ResponseBody
    public Result changeState(@RequestParam("courseId") Integer courseId,
                                @RequestParam("status") Integer status)  {
        log.info("更改课程状态courseId:{} status:{}",courseId,status);
        try {
            CourseDTO courseDTO = new CourseDTO();
            courseDTO.setId(courseId);
            courseDTO.setStatus(status);
            courseRemoteService.saveOrUpdateCourse(courseDTO);
            return Result.success();
        }catch (Exception e){
            log.error("获取课程信息:",e);
            return Result.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "分页查询课程信息")
    @PostMapping("getQueryCourses")
    @ResponseBody
    public Result getQueryCourses(@RequestBody CourseQueryParam courseQueryParam)  {
        log.info("分页查询课程信息:{}",JSON.toJSONString(courseQueryParam));
        try {
            PageResultDTO<CourseDTO> queryCourses = courseRemoteService.getQueryCourses(courseQueryParam);
            return Result.success(queryCourses);
        }catch (Exception e){
            log.error("分页查询课程信息:",e);
            return Result.fail(e.getMessage());
        }
    }



    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        UpLoadResult upLoadResult = ossService.upload(multipartFile);
        return Result.success(upLoadResult);
    }

}
